#include "behavior_tree_while_do_policy.h"
#include <behaviortree_cpp_v3/controls/while_node.h>

namespace policy
{
BehaviorTreeWhileDoPolicy::BehaviorTreeWhileDoPolicy(const std::string &name)
    : BehaviorTreePolicy(name)
{
    policy_tree_node_ = std::make_unique<BT::WhileNode>(name + "_while_do_node");
}
bool BehaviorTreeWhileDoPolicy::setWhileConditionTask(const task::AbstractTaskConstPtr &task)
{
    return BehaviorTreePolicy::insertTask(0, task);
}
bool BehaviorTreeWhileDoPolicy::setWhileConditionPolicy(const AbstractPolicyConstPtr &policy)
{
    return BehaviorTreePolicy::insertPolicy(0, policy);
}
bool BehaviorTreeWhileDoPolicy::setExecuteTask(const task::AbstractTaskConstPtr &task)
{
    return BehaviorTreePolicy::insertTask(1, task);
}
bool BehaviorTreeWhileDoPolicy::setExecutePolicy(const AbstractPolicyConstPtr &policy)
{
    return BehaviorTreePolicy::insertPolicy(1, policy);
}
void BehaviorTreeWhileDoPolicy::setEndCondition(task::TaskStatus status)
{
    auto node = dynamic_cast<BT::WhileNode *>(policy_tree_node_.get());
    if (nullptr == node)
    {
        throw std::runtime_error("behavior tree while do policy case fail");
    }
    switch (status)
    {
        case task::TaskStatus::Running:
            node->setEndCondition(BT::NodeStatus::RUNNING);
            break;
        case task::TaskStatus::Success:
            node->setEndCondition(BT::NodeStatus::SUCCESS);
            break;
        case task::TaskStatus::Failure:
            node->setEndCondition(BT::NodeStatus::FAILURE);
            break;
        default:
            EASE_ERROR("while do policy, unknown task status: %d", (int) status);
            break;
    }
}
BehaviorTreeWhileDoPolicy::~BehaviorTreeWhileDoPolicy() = default;
}
